'''
Created on 2011-5-19

@author: lchengh2
'''

import unittest
import os
import zipfile
import shutil

from myhomework.filemanager.deploymentsetting import DeploymentSetting
from myhomework.filemanager.filemanager import FileManager
from myhomework.account.daos import *
from myhomework.homework.daos import *

class  FileManagerTestCase(unittest.TestCase):

    def setUp(self):
        
        u1 = User(student_id='0001', name='user1')
        UserDao.add_user(u1)
        u2 = User(student_id='0002', name='user2')
        UserDao.add_user(u2)
        u3 = User(student_id='0003', name='user3')
        UserDao.add_user(u3)
        u4 = User(student_id='0004', name='user4')
        UserDao.add_user(u4)
        
        a_user = User.objects.get(student_id='0001')
        b_user = User.objects.get(student_id='0002')
        new_group = Group(administrator=a_user, name='group1')
        new_group.save()
        a_user.group = new_group
        b_user.group = new_group
        a_user.save()
        b_user.save()
 
        c_user = User.objects.get(student_id='0003')
        d_user = User.objects.get(student_id='0004')
        new_group2 = Group(administrator=c_user, name='group2')
        new_group2.save()
        c_user.group = new_group2
        d_user.group = new_group2
        c_user.save()
        d_user.save()       
        
        h1 = Homework(name='homework_01',
                      content='content_01',
                      deadline='2011-5-19',
                      type='personal')   
        HomeworkDao.add_homework(h1)
        h2 = Homework(name='homework_02',
                      content='content_02',
                      deadline='2011-6-19',
                      type='team')   
        HomeworkDao.add_homework(h2)

    def tearDown(self):
        HomeworkDao.delete_homework_by_name('homework_01')
        HomeworkDao.delete_homework_by_name('homework_02')
        
        for user in User.objects.all():
            user.auth_user.delete()
            user.delete()        
        
        for group in Group.objects.all():
            group.delete()      
    
    def test_save_homework_submitted(self):
        user1 = UserDao.get_user_by_student_id('0001')
        user2 = UserDao.get_user_by_student_id('0002')
        user3 = UserDao.get_user_by_student_id('0003')
        user4 = UserDao.get_user_by_student_id('0004')
        group1 = GroupDao.get_group_by_name('group1')
        group2 = GroupDao.get_group_by_name('group2')
        p_homework = HomeworkDao.get_homework_by_name('homework_01')
        t_homework = HomeworkDao.get_homework_by_name('homework_02')        
        list = []
        list.append(Contribution(user = user1,contribution = 50))
        list.append(Contribution(user = user2,contribution = 50))

        FileManager.initilize()
        
        result = []
        
        result = FileManager.get_ready_for_saving_homework(p_homework.id, '0001')
#        self.assertEqual(result[0],'myhomework/all_src_file/personal_homework/homework_01/0001_user1/0001_user1.zip')
#        self.assertEqual(result[1],'myhomework/all_homework_submited/personal_homework/homework_01/0001_user1/pic.png')     
        shutil.copy('myhomework/test_data/0001_user1.zip', result[0])
        FileManager.save_homework_submitted(p_homework.id, '0001')
        
        
        result = FileManager.get_ready_for_saving_homework(p_homework.id, '0001')
#        self.assertEqual(result[0],'myhomework/all_src_file/personal_homework/homework_01/0001_user1/0001_user1(1).zip')
#        self.assertEqual(result[1],'myhomework/all_homework_submited/personal_homework/homework_01/0001_user1/pic.png')
#        print(result[1])       
        shutil.copy('myhomework/test_data/0001_user1.zip', result[0])
        FileManager.save_homework_submitted(p_homework.id, '0001')

        result = FileManager.get_ready_for_saving_homework(p_homework.id, '0001')
#        self.assertEqual(result[0],'myhomework/all_src_file/personal_homework/homework_01/0001_user1/0001_user1(2).zip')
#        self.assertEqual(result[1],'myhomework/all_homework_submited/personal_homework/homework_01/0001_user1/pic.png')       
        shutil.copy('myhomework/test_data/0001_user1.zip', result[0])
        FileManager.save_homework_submitted(p_homework.id, '0001')  
        
        result = FileManager.get_ready_for_saving_homework(p_homework.id, '0002')
#        self.assertEqual(result[0],'myhomework/all_src_file/personal_homework/homework_01/0002_user2/0002_user2.zip')
#        self.assertEqual(result[1],'myhomework/all_homework_submited/personal_homework/homework_01/0002_user2/pic.png')
#        print(result[1])  
        shutil.copy('myhomework/test_data/0002_user2.zip', result[0])
        FileManager.save_homework_submitted(p_homework.id, '0002')
        
        result = FileManager.get_ready_for_saving_homework(t_homework.id, '0001')
#        self.assertEqual(result[0],'myhomework/all_src_file/teamwork/homework_02/group1/group1.zip')
#        self.assertEqual(result[1],'myhomework/all_homework_submited/teamwork/homework_02/group1/pic.png')
#        print(result[1]) 
        shutil.copy('myhomework/test_data/group1.zip', result[0])
        FileManager.save_homework_submitted(t_homework.id, '0001', list) 
        
        result = FileManager.get_ready_for_saving_homework(t_homework.id, '0001')
#        self.assertEqual(result[0],'myhomework/all_src_file/teamwork/homework_02/group1/group1(1).zip')
#        self.assertEqual(result[1],'myhomework/all_homework_submited/teamwork/homework_02/group1/pic.png')
#        print(result[1])
        shutil.copy('myhomework/test_data/group1.zip', result[0])
        FileManager.save_homework_submitted(t_homework.id, '0001', list)              
        
        self.assertEqual(len(HomeworkSubmitedDao.get_homework_submited_list(p_homework.id, user1.student_id)),3)
        self.assertEqual(len(HomeworkSubmitedDao.get_homework_submited_list(p_homework.id, user2.student_id)),1)
        self.assertEqual(len(HomeworkSubmitedDao.get_homework_submited_list(t_homework.id, user1.student_id)),2)
        self.assertEqual(len(HomeworkSubmitedDao.get_all_submitted_list(user1.student_id, 0, 100)),5)
        self.assertEqual(HomeworkSubmitedDao.get_all_submitted_count(user1.student_id),5)
        FileManager.remove_homework_submitted(p_homework, user1)
        FileManager.remove_homework_submitted(p_homework, user2)
        FileManager.remove_homework_submitted(t_homework, user1)
        FileManager.delete_homework_folder(p_homework)
        FileManager.delete_homework_source_folder(p_homework)   
        FileManager.delete_homework_folder(t_homework)
        FileManager.delete_homework_source_folder(t_homework)       

    
    
                